package com.yidaozou.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yidaozou.model.CmsFriendlinkType;
import com.yidaozou.service.CmsFriendlinkTypeService;
import com.yidaozou.util.DataStructureUtil;

@Controller
@RequestMapping("/type")
public class CmsFriendlinkTypeAction {

	private Logger logger = LoggerFactory.getLogger(CmsFriendlinkTypeAction.class);

	@Autowired
	private CmsFriendlinkTypeService cmsFriendlinkTypeService;
	
	/**
	 * 服务提供
	 * @param request
	 * @param response
	 */
	@RequestMapping("service")
	public void service(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("In service ...");
	}

	/**
	 * 操作数据库测试
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request, HttpServletResponse response) {
		CmsFriendlinkType type = new CmsFriendlinkType();
		type.setName("天天");
		type.setNumber("12123");
		type.setSeq(1);
		type.setSiteId(1);
		cmsFriendlinkTypeService.insertCmsFriendlinkType(type);
		return null;
	}

	/**
	 * 接受并相应报文测试
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("add")
	public void add(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("UTF-8");
			// 解析报文
			BufferedReader reader = request.getReader();
			String line = null;
			StringBuffer inputString = new StringBuffer();
			while ((line = reader.readLine()) != null) {
				inputString.append(line);
			}
			System.out.println("接收到的报文：["+inputString+"]");

			CmsFriendlinkType type = DataStructureUtil.json2Bean(inputString.toString(), CmsFriendlinkType.class);
			int result = cmsFriendlinkTypeService.insertCmsFriendlinkType(type);
			String rtStr = null;
			if (result == 1) {
				rtStr = "{errorCode:000000,errorMsg:success}";
			} else {
				rtStr = "{errorCode:999999,errorMsg:fail}";
			}
			// 返回报文
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");

			StringBuffer resultString = new StringBuffer();
			resultString.append(rtStr);
			PrintWriter out = response.getWriter();
			out.print(resultString.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.error("error", e);
		} catch (Exception e) {
			logger.error("error", e);
		} finally {

		}
	}
	
	@RequestMapping("add2")
	public void add2(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("UTF-8");
			String msg = request.getParameter("msg");
			String info = request.getParameter("info");
			System.out.println("msg=" + msg);
			System.out.println("info=" + info);
			
			CmsFriendlinkType type = DataStructureUtil.json2Bean(msg, CmsFriendlinkType.class);
			int result = cmsFriendlinkTypeService.insertCmsFriendlinkType(type);
			String rtStr = null;
			if (result == 1) {
				rtStr = "{errorCode:000000,errorMsg:success}";
			} else {
				rtStr = "{errorCode:999999,errorMsg:fail}";
			}
			
			PrintWriter out = response.getWriter();
			out.print(rtStr);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			logger.error("error", e);
		} catch (IOException e) {
			logger.error("error", e);
		} catch (Exception e) {
			logger.error("error", e);
		}
	}
	
	
	@RequestMapping("add3")
	public void add3(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("UTF-8");
			String msg = request.getParameter("msg");
			String info = request.getParameter("info");
			System.out.println("msg=" + msg);
			System.out.println("info=" + info);
			
			net.sf.json.JSONObject jsonObj = net.sf.json.JSONObject.fromObject(msg);
			CmsFriendlinkType type = (CmsFriendlinkType)net.sf.json.JSONObject.toBean(jsonObj, CmsFriendlinkType.class);
			int result = cmsFriendlinkTypeService.insertCmsFriendlinkType(type);
			String rtStr = null;
			if (result == 1) {
				rtStr = "{errorCode:000000,errorMsg:success}";
			} else {
				rtStr = "{errorCode:999999,errorMsg:fail}";
			}
			
			PrintWriter out = response.getWriter();
			out.print(rtStr);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			logger.error("error", e);
		} catch (IOException e) {
			logger.error("error", e);
		} catch (Exception e) {
			logger.error("error", e);
		}
	}
	
	/**
	 * 接受并相应报文测试
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("add4")
	public void add4(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("UTF-8");
			// 解析报文
			BufferedReader reader = request.getReader();
			String line = null;
			StringBuffer inputString = new StringBuffer();
			while ((line = reader.readLine()) != null) {
				inputString.append(line);
			}
			System.out.println("接收到的报文：["+inputString+"]");

			net.sf.json.JSONObject jsonObj = net.sf.json.JSONObject.fromObject(inputString.toString());
			CmsFriendlinkType type = (CmsFriendlinkType)net.sf.json.JSONObject.toBean(jsonObj, CmsFriendlinkType.class);
			
			int result = cmsFriendlinkTypeService.insertCmsFriendlinkType(type);
			String rtStr = null;
			if (result == 1) {
				rtStr = "{errorCode:000000,errorMsg:success}";
			} else {
				rtStr = "{errorCode:999999,errorMsg:fail}";
			}
			// 返回报文
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");

			StringBuffer resultString = new StringBuffer();
			resultString.append(rtStr);
			PrintWriter out = response.getWriter();
			out.print(resultString.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.error("error", e);
		} catch (Exception e) {
			logger.error("error", e);
		} finally {

		}
	}
}
